package com.suning.sawp.member.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.dto.customer.CustshopFrequency;
import com.suning.sawp.intf.esb.ShopFrequencyService;
import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.dao.ActivityDao;
import com.suning.sawp.member.dao.StoreExtensionDao;
import com.suning.sawp.member.dto.Activity;
import com.suning.sawp.member.service.LabelService;
import com.suning.sawp.member.service.StoreExpandService;
import com.suning.sawp.member.vo.CateExpandDetailVo;
import com.suning.sawp.member.vo.CustLabelVo;
import com.suning.sawp.member.vo.EmpExpandDetailVo;
import com.suning.sawp.member.vo.MemberExtensionCateVo;
import com.suning.sawp.member.vo.MemberExtensionClientVo;
import com.suning.sawp.member.vo.MemberExtensionEmpVo;
import com.suning.sawp.member.vo.MemberExtensionStatisVo;
import com.suning.sawp.member.vo.StoreExpandDetailVo;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.service.util.DateUtils;

/**
 * 
 * 门店外拓服务实现 <br>
 *
 * @author 14060886 祝翔龙
 */
@Service
public class StoreExpandServiceImpl implements StoreExpandService {

	@Autowired
	private StoreExtensionDao storeExtensionDao;
	@Autowired
	private ShopFrequencyService shopFrequencyService;
	@Autowired
	private ActivityDao activityDao;

	@Autowired
	private LabelService labelService;

	@Override
	public StoreExpandDetailVo queryExpandDetailByStore(String activityId, String storeCode, String dateType) {
		StoreExpandDetailVo result = new StoreExpandDetailVo();
		// 查询品类外拓信息
		List<MemberExtensionCateVo> cateList = storeExtensionDao.selectExpandCateInfoByStore(activityId, storeCode, dateType, MemberConstants.STORE_EXPEND);
		if (CollectionUtils.isEmpty(cateList)) {
			cateList = new ArrayList<MemberExtensionCateVo>();
		}
		result.setCateList(cateList);
		// 查询统计外拓信息
		MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByStore(activityId, storeCode, dateType, MemberConstants.STORE_EXPEND);
		if (null == statistics) {
			statistics = new MemberExtensionStatisVo();
		}
		if (dateType.equals(MemberConstants.EXPAND_PERIOD)) {
			// 查询目标外拓人数
			Integer targetPeople = storeExtensionDao.selectTargetPeopleByStore(activityId, storeCode, null);
			statistics.setTargetPeople(null == targetPeople ? null : String.valueOf(targetPeople));
		}
		setActivityTime(statistics, activityId, dateType);
		result.setStatistic(statistics);
		return result;
	}

	@Override
	public CateExpandDetailVo queryExpandDetailByCate(String activityId, String storeCode, String cateCode) {
		CateExpandDetailVo result = new CateExpandDetailVo();
		// 查询店员外拓信息(品类)
		List<MemberExtensionEmpVo> employeeList = storeExtensionDao.selectExpandEmpInfoByCate(activityId, storeCode, cateCode, MemberConstants.EXPAND_PERIOD, MemberConstants.STORE_EXPEND);
		if (CollectionUtils.isEmpty(employeeList)) {
			employeeList = new ArrayList<MemberExtensionEmpVo>();
		}
		result.setEmployeeList(employeeList);
		MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByCate(activityId, storeCode, cateCode, MemberConstants.EXPAND_PERIOD, MemberConstants.STORE_EXPEND);
		if (null == statistics) {
			statistics = new MemberExtensionStatisVo();
		}
		// 查询目标外拓人数
		Integer targetPeople = storeExtensionDao.selectTargetPeopleByStore(activityId, storeCode, cateCode);
		statistics.setTargetPeople(null == targetPeople ? null : String.valueOf(targetPeople));
		setActivityTime(statistics, activityId, MemberConstants.EXPAND_PERIOD);
		result.setStatistic(statistics);
		return result;
	}

	@Override
	public CateExpandDetailVo queryExpandDetailByLoc(String activityId, String storeCode, String expandId) {
		CateExpandDetailVo result = new CateExpandDetailVo();
		// 查询店员外拓信息(地点)
		List<MemberExtensionEmpVo> employeeList = storeExtensionDao.selectExpandEmpInfoByLoc(activityId, storeCode, expandId, MemberConstants.EXPAND_PERIOD, MemberConstants.STORE_EXPEND);
		if (CollectionUtils.isEmpty(employeeList)) {
			employeeList = new ArrayList<MemberExtensionEmpVo>();
		}
		result.setEmployeeList(employeeList);
		MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByLoc(activityId, storeCode, expandId, MemberConstants.STORE_EXPEND);
		if (null == statistics) {
			statistics = new MemberExtensionStatisVo();
		}
		// 查询目标外拓人数
		Integer targetPeople = storeExtensionDao.selectTargetPeopleByLoc(expandId);
		statistics.setTargetPeople(null == targetPeople ? null : String.valueOf(targetPeople));
		setActivityTime(statistics, activityId, MemberConstants.EXPAND_PERIOD);
		result.setStatistic(statistics);
		return result;
	}

	@Override
	public EmpExpandDetailVo queryExpandDetailByEmp(String activityId, String storeCode, String employeeId, String expandId, String pageNo) {
		EmpExpandDetailVo result = new EmpExpandDetailVo();
		List<MemberExtensionClientVo> clientList = storeExtensionDao.selectExpandClientInfoByEmp(activityId, storeCode, employeeId, expandId, MemberConstants.STORE_EXPEND, null,
				Integer.valueOf(pageNo), MemberConstants.PAGE_SIZE);
		List<String> custList = new ArrayList<String>();
		if (CollectionUtils.isEmpty(clientList)) {
			clientList = new ArrayList<MemberExtensionClientVo>();
		} else {
			for (MemberExtensionClientVo client : clientList) {
				custList.add(client.getCustNo());
			}
		}
		if (CollectionUtils.isNotEmpty(custList)) {
			// 查询会员首购复购接口
			Map<String, String> frequencyList = shopFrequencyService.queryShopFrequency(custList);
			for (MemberExtensionClientVo client : clientList) {
				client.setBuyStatus(processBuyStatus(client.getCustNo(), frequencyList));
				client.setMobile(getPhone(client.getMobile()));

				// 查询标签列表
				List<CustLabelVo> custLabels = labelService.queryFiveLabelByCustId(Long.parseLong(client.getCustId()));
				client.setCustLabels(custLabels);
			}
		}
		result.setCustomerList(clientList);
		if (MemberConstants.ONE_PAGE.intValue() == Integer.parseInt(pageNo)) {
			MemberExtensionStatisVo statistics = storeExtensionDao.selectExpandStatisInfoByEmp(activityId, storeCode, employeeId, expandId, MemberConstants.STORE_EXPEND, null);
			if (null == statistics) {
				statistics = new MemberExtensionStatisVo();
			}
			// 查询目标外拓人数
			Integer targetPeople = storeExtensionDao.selectTargetPeopleByEmp(activityId, storeCode, employeeId, expandId);
			statistics.setTargetPeople(null == targetPeople ? null : String.valueOf(targetPeople));
			setActivityTime(statistics, activityId, MemberConstants.EXPAND_PERIOD);
			result.setStatistic(statistics);
		}
		return result;
	}

	private void setActivityTime(MemberExtensionStatisVo statistics, String activityId, String dateType) {
		// 查询活动信息
		Activity activity = activityDao.selectActivityInfoById(activityId);
		if (null != activity) {
			statistics.setActivityName(activity.getName());
			if (dateType.equals(MemberConstants.EXPAND_PERIOD)) {
				// 任务开始时间为活动创建时间 结束时间为活动开始时间前一天
				statistics.setStartTime(DateUtils.getDateStrByFormat(activity.getCreateTime(), DateUtils.DEFAULT_DATE_PATTERN));
				statistics.setEndTime(DateUtils.getDateStrByFormat(DateUtils.getNextDay(activity.getStartTime(), -1), DateUtils.DEFAULT_DATE_PATTERN));
			} else {
				statistics.setStartTime(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_DATE_PATTERN));
				statistics.setEndTime(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_DATE_PATTERN));
			}
		}
	}

	private String processBuyStatus(String custNo, Map<String, String> frequencyList) {
		if (StringUtils.isBlank(custNo) || frequencyList.isEmpty()) {
			return CustshopFrequency.DealType.NOT_MEMBER;
		} else {
			// 获取客户购物状态对象
			CustshopFrequency custshopFrequency = DJStringUtils.getDealType(frequencyList.get(custNo));
			return custshopFrequency.getDealType();
		}
	}

	private String getPhone(String phone) {
		if (StringUtils.isNotEmpty(phone)) {
			return AESUtils.encrypt(phone, ConfigConstants.AES_KEY);
		}
		return StringUtils.EMPTY;
	}

}
